<template>
  <div class="main">
    <div class="top-main">
      <dz-nav :is-back="true" :back-url="'/my'">
        <template #center>
          <b v-color><qq>Recharge</qq></b>
        </template>
      </dz-nav>
    </div>
    <div class="recharge">
      <div class="payments">
        <div class="item"
          v-for="(item, index) in payChannels" :key="index"
             :class="{'active': curChannelIndex === index}"
             @click="chooseChannel(index)"
        >
          <img :src="item.img" alt="" class="icon">
          <p class="name over-hide-1">{{item.name}}</p>
        </div>
      </div>
      <div class="money">
        <div class="number">
          <input type="text" v-model="formData.amount">
        </div>
        <div class="btn">
          <img src="@/assets/images/button-+.png" alt="" @click="handleAdd">
          <img src="@/assets/images/button--.png" alt="" @click="handleSub">
        </div>
      </div>
      <div class="bottom-box">
        <div class="btn" @click="handleRecharge">
          <qq>Recharge</qq>
        </div>
      </div>
    </div>
    <div class="list">
      <div class="noData" v-if="lists.length<1">
        <img src="@/assets/images/img_search_result.png" alt="" class="img-noData">
        <p><qq>Norecords</qq></p>
      </div>
      <van-list
          v-else
          style="width: 100%;"
          v-model:loading="loading"
          :finished="finished"
          finished-text="Finished"
          loading-text="loading"
          error-text="error"
          @load="onLoad"
      >
        <div class="lists">
          <div class="item">
            <p>
              <span class="desc">dcdvfdcdvfdvfvfbvfgbgdcdvfdcdvfdvfvfbvfgbg</span>
              <span class="money">110 USDT</span>
            </p>
            <p>
              <span>28-05-2024 15:41:57</span>
              <span class="span">In Payment</span>
            </p>
          </div>
        </div>
      </van-list>

    </div>
  </div>
</template>

<script>
import {rechargeList, payChannel, createRechargeOrder, getChannelTypeIsopen} from "@/api/apiFox";
import {Toast} from "vant";
import {setSessionItem} from "@/utils/auth";

export default {
  data(){
    return{
      curChannelIndex: 0,
      curChannel: {},
      payMethod:[],
      payChannels: [],
      pageNo: 0,
      count: 0,
      finished: false,
      loading: false,
      initFinish: false,
      lists: [],
      formData: {
        amount: 100
      }
    }
  },
  methods: {
    getPayChannels() {
      payChannel().then(res => {
        this.payMethod = res.data
      }).catch(err => {})
    },
    chooseChannel(index) {
      this.curChannelIndex = index
      this.curChannel = this.payChannels[this.curChannelIndex]
    },
    handleAdd() {
      this.formData.amount += 100
    },
    handleSub() {
      if(this.formData.amount - 100 > 0) {
        this.formData.amount -= 100
      }else{
        Toast(this.$t('Theamountcannotbereducedanyfurther'))
      }
    },
    handleRecharge() {
      if (this.curChannel.id === 6){
        console.log(this.curChannel)
        this.$router.push({path:'/payment',query:{money:this.formData.amount}}).catch(err=>{})
      }else{
        let f = {
          amount: this.formData.amount,
          channelName: this.payMethod[0].channelName,
          channelType:this.curChannel.channelType
        }
        createRechargeOrder(f).then(res => {
          setSessionItem('pay',res.data)
          this.$router.push({path:'/pay'}).catch(err=>{})
        }).catch(err => {})
      }
    },

    getLists() {
      rechargeList({ pageNo: this.pageNo, pageSize: 15 }).then(res => {
        this.lists = this.lists.concat(res.data)
        this.count = res.count
        this.initFinish = true
        this.loading = false
      }).catch(err => {})
    },
    onLoad() {
      if(this.pageNo === 0) {
        this.pageNo ++
        this.getLists()
      }else{
        if(this.lists.length < this.count) {
          this.pageNo ++
          this.getLists()
        }else{
          this.finished = true
        }
      }
    },

    getPay(){
      getChannelTypeIsopen().then(res=>{
        let s = [
          {id:1,name:'USDT.TRC20',img:require('@/assets/images/TRC20.png'),channelType:res.trc20,isOpen:res.withdrawal_trc20},
          {id:2,name:'USDT.ERC20',img:require('@/assets/images/TRC20.png'),channelType:res.erc20,isOpen:res.withdrawal_erc20},
          {id:3,name:'USDT.Polygon',img:require('@/assets/images/Polygon.png'),channelType:res.polygon,isOpen:res.withdrawal_polygon},
          {id:4,name:'USDC.Polygon',img:require('@/assets/images/Polygon.png'),channelType:res.usdc_polygon,isOpen:res.withdrawal_usdc_polygon},
          {id:5,name:'ETH',img:require('@/assets/images/Property1=USDT.png'),channelType:res.default,isOpen:1},
          // {id:6,name:'balance pay',img:require('@/assets/images/bt.png'),channelType:res.default,isOpen:1},
        ]
        this.payChannels = s.filter(value => {return parseInt(value.isOpen) === 1})
        if (this.payChannels && this.payChannels.length>0){
          this.curChannel = this.payChannels[0]
        }
      }).catch(err=>{})
    }
  },

  created() {
    this.getPayChannels()
  },
  mounted() {
    this.getPay()
  },
}
</script>

<style scoped lang="scss">
.main{
  width: 100%;
  min-height: 100vh;
  background: url("@/assets/images/bg.jpg") top center no-repeat;
  background-attachment: fixed;
  background-size: 100% 100%;
  .top-main{
    width: 100%;
    .avatar{
      padding-top: 10px;
      text-align: center;
      .img-avatar{
        width: 180px;
        height: 180px;
      }
    }
  }
  .recharge{
    padding: 15px 15px 0;
    .payments{
      display: flex;

      overflow-x: auto;
      /* 隐藏滚动条 */
      scrollbar-width: none; /* firefox */
      -ms-overflow-style: none; /* IE 10+ */
      .item{
        width: 109px;
        height: 92px;
        background: #FFFFFF;
        box-shadow: 0px 4px 4px 0px rgba(160,58,58,0.25);
        border-radius: 70px 70px 70px 70px;
        padding: 16px 0;
        text-align: center;
        box-sizing: border-box;
        margin-right: 8px;
        display: inline-block;
        flex-grow: 0;
        flex-shrink: 0;
        &:last-child{
          margin-right: 0;
        }
        &.active{
          background: linear-gradient( 180deg, #8372FF 0%, #533DF1 100%);
          box-shadow: 0px 4px 4px 0px rgba(160,58,58,0.25);
          .name{
            color: #FFFFFF;
          }
        }
        .icon{
          width: 40px;
          height: 40px;
          //outline: 1px dashed red;
        }
        .name{
          font-weight: bold;
          font-size: 12px;
          color: #533DF1;
          //outline: 1px dashed red;
        }
      }
    }
    .money{
      height: 170px;
      background: url("@/assets/images/CARD.png") center center no-repeat;
      background-size: 100% 100%;
      margin-top: 20px;
      .number{
        height: 140px;
        text-align: center;
        line-height: 150px;
        font-size: 36px;
        color: #fff;
        font-weight: bold;
        input{
          width: 100%;
          text-align: center;
        }
      }
      .btn{
        display: flex;
        justify-content: space-between;
        img{
          width: 49.5%;
          height: 30px;
        }
      }
    }
    .bottom-box{
      width: 100%;
      height: 100px;
      display: flex;
      justify-content: center;
      align-items: center;
      .btn{
        width: 343px;
        height: 50px;
        background: linear-gradient( 90deg, #973DF1 0%, #5134C3 100%);
        box-shadow: 0px 4px 4px 0px rgba(92,18,187,0.25), inset 0px 4px 4px 0px rgba(249,249,249,0.25);
        border-radius: 30px 30px 30px 30px;
        font-weight: bold;
        font-size: 16px;
        color: #FFFFFF;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }
  }
  .list{
    height: 500px;
    background: linear-gradient( 180deg, #FFF0C9 0%, #D49188 100%);
    border-radius: 30px 30px 0px 0px;
    padding: 20px 15px;
    box-sizing: border-box;
    .noData{
      text-align: center;
      font-size: 14px;
      color: rgba(0,0,0,0.3);
      padding-top: 100px;
      img{
        width: 140px;
        height: 140px;
      }
    }
    .lists{
      height: 460px;
      padding: 0 15px;
      background: #FFFFFF;
      border-radius: 20px;
      box-shadow: 0px 2px 4px 0px rgba(31,33,88,0.25);
      box-sizing: border-box;
      .item{
        padding: 10px 0;
        border-bottom: 1px solid rgba(133,133,138,0.2);
        p{
          display: flex;
          justify-content: space-between;
          font-size: 12px;
          color: #5B6B86;
          &:first-child{
            margin-bottom: 5px;
          }
          .desc{
            font-size: 14px;
            color: #242B35;
            word-break: break-all;
          }
          .money{
            font-weight: bold;
            font-size: 14px;
            color: #533DF1;
            flex-shrink: 0;
            margin-left: 20px;
          }
        }
      }
    }
  }
}
</style>